<?php

/**
 * IndexController is the default controller for this application
 */
class IndexController extends BrowseControllerBase {
	/**
	 * BannerDataService object
	 *
	 * @var BannerDataService object
	 */
	public $bannerDataService = null;
	
	public $currentUserId = null;
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		$this->currentUserId = $this->authentication->getCurrentUserId ();
		// Set page name
		$this->setPageName ( 'home' );
		
		// Initialise AdminBlogDataService
		$this->adminBlogDataService = new AdminBlogDataService ();
		
		// Save urer history which he is visiting
		$this->userSessionVisitDataService->saveHistoryForCurrentUser ( USER_VISIT_HOMEPAGE );
		
		// Get SettingsDataService
		$settingsDataService = new SettingsDataService ();
		$this->setPageTitle ( $settingsDataService->getValue ( SETTINGS_HOMEPAGE_TITLE ) );
		$this->setMetaDescription ( $settingsDataService->getValue ( SETTINGS_HOMEPAGE_META_DESCRIPTION ) );
		$this->setMetaKeywords ( $settingsDataService->getValue ( SETTINGS_HOMEPAGE_META_KEYWORDS ) );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		if (isset ( $_COOKIE ['city'] )) {
			$city = $_COOKIE ['city'];
		} else {
			$city = 1;
		}
		if($this->getPost('button', '') != ''){
			$email = $this->getPost('email','');
			$subDataService = new SubscribeDataService();
			$subEntity = $subDataService->beforeCreate ();
			$subEntity->email = $email;
			$subDataService->create($subEntity);
		}
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Deals/Index/city/' . $city;
		CommonUtils::redirect ( $url );
		return - 1;
	}
	
	public function termandconditionAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/TermAndCondition/term_condition.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function error404Action() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Index/404.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function errorAuthorAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Index/not_authorized.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Handle Website Expired
	 */
	public function expiredAction() {
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Index/expired.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	public function loginAction() {
		//city
		$city = $this->getQuery ( 'city' );
		if ($city == '') {
			$city = $this->getPost ( 'city', 1 );
		}
		if ($return_url = $this->getQuery ( 'return_url' )) {
			$this->smarty->assign ( 'return_url', $return_url );
		}
		if ($this->getPost ( 'login' )) {
			
			$email = $this->getPost ( 'email', '' );
			$password = $this->getPost ( 'password', '' );
			// Try to login this user
			$userDataService = new UserDataService ();
			$result = $userDataService->login ( $email, $password );
			if ($result != SUCC) {
				$message = "Có lỗi xảy ra!";
			} else {
				if ($return_url = $this->getPost ( 'return_url' )) {
					$return_url = str_replace ( 'EquaL', '=', $return_url );
					$return_url = str_replace ( 'AnD', '&', $return_url );
					$return_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/index.php?' . $return_url;
					
					CommonUtils::redirect ( $return_url );
				}
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Deals/Index/city/' . $city;
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		//if not login, redierect to register or signin
		if ($this->currentUserId != null && $this->authentication->getUserType () == ROLE_MEMBER) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Deals/Index/city/' . $city;
			CommonUtils::redirect ( $url );
			return - 1;
		}
		$this->smarty->assign ( 'city', $city );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Index/login.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function registerAction() {
		//city
		$city = $this->getQuery ( 'city' );
		if ($city == '') {
			$city = $this->getPost ( 'city', 1 );
		}
		
		$result = SUCC;
		$message = 'Signup Fails. Please try again!';
		if ($this->getPost ( 'resgister' )) {
			$email = $this->getPost ( 'email', '' );
			$password = $this->getPost ( 'password', '' );
			$confirmPassword = $this->getPost ( 'confirmPassword', '' );
			$fullname = $this->getPost ( 'fullname', '' );
			$gender = $this->getPost('gender', '');
			$birthday =  $this->getPost('birthday', '');
			if ($password != $confirmPassword) {
				$result = ERROR;
				$message = 'Password mismatch! Please try again!';
				$this->smarty->assign ( 'message', $message );
			}
			$userDataService = new UserDataService ();
			if ($userDataService->checkExistEmail ( $email ) == SUCC) {
				$result = ERROR;
				$message .= '$email is exist in system !';
			
			}
			if ($result != ERROR) {
				$authenticate = new Authentication ();
				$userEntity = $userDataService->beforeCreate ();
				$userEntity->email = $email;
				$userEntity->fullname = $fullname;
				$userEntity->gender = $gender;
				$userEntity->birthday = $birthday;
				$userEntity->password = $authenticate->encryptPassword ( $password );
				$userEntity->status = UserEntity::ACTIVE;
				$userEntity->userType = ROLE_MEMBER;
				$userEntity->balance = 0;
				$userId = $userDataService->create ( $userEntity );
				// Try to login this user
				$result = $userDataService->login ( $email, $password );
				if ($result != SUCC) {
					$message = "Có lỗi xảy ra!";
				} else {
					$result = SUCC;
					$message = "Đăng ký thành công";
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Deals/Index/city/' . $city;
					CommonUtils::redirect ( $url );
					return - 1;
				}
			}
		}
		//if login, redierect to buydeal
		if ($this->currentUserId != null && $this->authentication->getUserType () == ROLE_MEMBER) {
			// Go on to buydeal
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		$this->smarty->assign ( 'msg', 'asdasd' );
		$this->smarty->assign ( 'isError', true );
		$this->smarty->assign ( 'city', $city );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Index/register.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
